var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
var axios = require("../util/request.js")
var app = getApp()
Page({
  data: {
    tabs: ["日环比", "周环比", "月环比"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    token: '',
    newday: '',
    startTime: '',
    endTime: '',
    startTime1: '',
    endTime1: '',
    startTime3: '',
    endTime3: '',
    hiddstartTime3: '',
    hiddendTime3: '',
    type: '0',
    data: '',
    hiddenmodalput: false,
    hiddenmodalput1: false,
    time: '',
    amount: '',
    num: '',
    tempimg: '',
    tempimg1: false,
    base6: '',
    uptype: '',
    red: '',
    start: '',
    tabBar: '',
    id: '',
    select: false,
    tihuoWay: '',
    array: '',
    array1: '',
    index: '',
    index1: '',
    shoptype: '1',
    shop_id: '',
    shopname1: "",
    monthday: '',
    monthday1:'',
    startTimemonth: '',
    startTimemonth1:''
  },
  
  onLoad: function () {
    this.time()
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id, 
      data:[],
      endTime3: '请选择时间',
      startTime3: '请选择时间',
      endTime: '请选择时间',
      startTime: '请选择时间'
    });
  },
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  // 门店确定
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    // console.log(this.data.array1[e.detail.value].shop_id)
    var shopname = this.data.array1[e.detail.value].shop_name
    // console.log(shopname)
    var that=this
        this.setData({
          index1:shopname,
          shop_id: that.data.array1[e.detail.value].shop_id
    })
    var dataarray1 = this.data.array1

    dataarray1.forEach((res) => {
      if (res.shop_name == shopname) {
        // console.log(res)
        this.setData({
          shop_id: res.shop_id

        })
      }
    })
    if (shopname == '所有') {
      var data = {
        "start_month": this.data.startTime,
        "end_month": this.data.startTime,
        'shop_id': ''
      }
      this.setData({
        shoptype: '1',
        index: this.data.array1[e.detail.value].shop_name,
      })
    } else {
      var data = {
        "start_month": this.data.startTime,
        "end_month": this.data.endTime,
        'shop_id': this.data.shop_id
      }
      this.setData({
        shoptype: '2',
        index: this.data.array1[e.detail.value].shop_name,
        data: '',


      })
    }
    
 

  },
  mySelect(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      tihuoWay: name,
      select: false
    })
  },
  //给点击的日期设置一个背景颜色
  bindDateChange1: function (e) {
    
    this.getLastDay(e.detail.value.split('-')[0],e.detail.value.split('-')[1],'1')
    if (e.detail.value <= this.data.endTime1) {
      
    } else {
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
    }

    this.setData({
      startTime1: e.detail.value,
      startTimemonth: e.detail.value,
      type: '1'
    })
  },
  bindDateChange5: function (e) {

    this.getLastDay(e.detail.value.split('-')[0], e.detail.value.split('-')[1], '1')
    if (e.detail.value <= this.data.endTime) {

    } else {
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
    }

    this.setData({
      startTime: e.detail.value,
      startTimemonth: e.detail.value,
      type: '1'
    })
  },
  bindDateChange6: function (e) {
    this.getLastDay(e.detail.value.split('-')[0], e.detail.value.split('-')[1], '2')
    this.setData({
      endTime: e.detail.value,
      startTimemonth1: e.detail.value,
      type: '2'
    })
    var data = {
      "end_month": e.detail.value,
      "start_month": this.data.startTime,
      'shop_id': this.data.shop_id
    }
    if (e.detail.value >= this.data.startTime) {

    } else {
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
    }

  },
  bindDateChange2: function (e) {
    this.getLastDay(e.detail.value.split('-')[0], e.detail.value.split('-')[1],'2')
    this.setData({
       endTime1: e.detail.value,
      startTimemonth1: e.detail.value,
      type: '2'
    })
    var data = {
      "end_month": e.detail.value,
      "start_month": this.data.startTime,
      'shop_id': this.data.shop_id
    }
    if (e.detail.value >= this.data.startTime1) {
     
    } else {
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
    }
  
  },
  // 月份格式化
  getLastDay(year, month,a) {
    var new_year = year;  //取当前的年份   
    var new_month = month++;//取下一个月的第一天，方便计算（最后一天不固定）   
    if (month > 12)      //如果当前大于12月，则年份转到下一年   
    {
      new_month -= 12;    //月份减   
      new_year++;      //年份增   
    }
    var new_date = new Date(new_year, new_month, 1);        //取当年当月中的第一天   
   
    if(a=='1'){
      this.setData({
        monthday: (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate()
      })
    }else{
      this.setData({
        monthday1: (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate()
      })
    
    }
    return (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate();//获取当月最后一天日期   
  } ,

  bindDateChange3: function (e) {

    var data = {
      "start_month": e.detail.value,
      "end_month": e.detail.value,
      "shop_id": ''
    }

 


    this.setData({
      startTime: e.detail.value,
      type: '1'
    })
  },

  //  请求日期接口
  dateinterface(data) {
    axios.post('yunpiaobox/shop_ring_ratios', data, this.shuffleSuc);
  },
  //  请求门店
  shoplist(data) {
    axios.getData('mall_manager/shop_list', this.shoplist1);
  },
  shoplist1(res) {
    // console.log(res.data.data.items)
  
    // shoulist.forEach((res1) => {
    //   // console.log(res1.shop_name)
    //   list.push(res1.shop_name)
    // })
    this.setData({
     
      array1: res.data.data.items
    })
  },

  // 得到当前日期
  double(num) {//让单数变成双数
    if (num < 10) {
      return '0' + num;//单数前面添加0
    } else {
      return num;
    }
  },
  time() {
    var that = this;
    var date = new Date();//创建一个日期对象
    var year = date.getFullYear()//获取4位年份
    var month = date.getMonth() + 1;//+1
    var day = date.getDate();
    var str = '';
    var str1 = '';
    var hour = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    str = year + '-' + this.double(month)
    str1 = year + '-' + this.double(month) + '-' + this.double(day)
    this.setData({
      newday: str
    })

    this.setData({
      endTime1: str1,
      startTime1: str1,
      endTime3: '请选择时间',
      startTime3: '请选择时间',
      endTime: '请选择时间',
      startTime: '请选择时间'
    })
    var data = {
      "end_month": str,
      "start_month": str,
      "shop_id": ''
    }
    setTimeout(function () {
  
      that.shoplist()
    }, 100)
    // 最大选择90天数据
    var date1 = new Date();
    var date2 = new Date(date1);
    date2.setDate(date1.getDate() - 266);
    var state = date2.getFullYear() + "-" + (date2.getMonth() + 1)
    this.setData({
      start: state

    })
    // console.log(state);
  },

  amount(a) {
    this.setData({
      amount: a.detail.value
    })

  },
  num(a) {
    this.setData({
      num: a.detail.value
    })

  },

  

  shuffleSuc(res) {
    if (res.data.code == 'E000000') {
        
      const data_items = res.data.data.items

      this.setData({
        data: res.data.data
      })
    } else {
      wx.showLoading({
        title: '暂无数据',
        mask: true
      })
      setTimeout(() => {
        wx.hideLoading()
      }, 500)
    }
    // console.log(res)
  },
  // 周环比
  bindDateChange3:function(e){
    this.getWeek(e.detail.value,'1')
    this.firstDayOfWeek(e.detail.value, '1')
  },
  bindDateChange4: function (e) {
    this.getWeek(e.detail.value, '2')
    this.firstDayOfWeek(e.detail.value, '2')
  },
  // 格式化获取第几周
  getWeek(dt,a) {
    let d1 = new Date(dt);
    let d2 = new Date(dt);    
    d2.setMonth(0);
    d2.setDate(1);
    let rq = d1 - d2;
    let days = Math.ceil(rq / (24 * 60 * 60 * 1000));
    let num = Math.ceil(days / 7);
    let year = dt.split('-')[0]
    if(a=='1'){
      this.setData({
        startTime3: year + "第" + num + '周',  
      })
    }else{
      this.setData({
        endTime3: year + "第" + num + '周',
        
      })
    }  
  },
  // 格式化周
  firstDayOfWeek(a,b) {
     console.log(a)
    var startDate = a; //获取输入的一个日期 
    var date = new Date(startDate);
    date.setDate(date.getDate() + 1 - date.getDay());
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
   var firstDateOfWeek = year + "-" + this.double(month) + "-" + this.double(day);
    
    if(b=='1'){
      
      this.setData({
        hiddstartTime3: firstDateOfWeek,

      })
    }else{
     
      this.setData({
        hiddendTime3: firstDateOfWeek
      })
    }
    
    return firstDateOfWeek
  },
  buttom:function(a){
    if (this.data.index == '' || this.data.index === null || this.data.index==undefined){
      wx.showToast({
        title: '请选择门店',
        icon: 'none',
        duration: 2000
      })
   }else{ 
      if (this.data.activeIndex===0){
        var data={
          "base_day": this.data.startTime1,
          "chose_day": this.data.endTime1,
          "feature": "day",
          "shop_id": this.data.shop_id
        }
        this.dateinterface(data)
      } else if (this.data.activeIndex == "1") {
        if (this.data.hiddstartTime3 == '' || this.data.hiddstartTime3 === null || this.data.hiddstartTime3===undefined){
          wx.showToast({
            title: '请选择基准',
            icon: 'none',
            duration: 2000
          })
        } else if (this.data.hiddendTime3 == '' || this.data.hiddendTime3 === null || this.data.hiddendTime3 === undefined){
          wx.showToast({
            title: '请选择时间',
            icon: 'none',
            duration: 2000
          })
        }else{
          var data = {
            "base_day": this.data.hiddstartTime3,
            "chose_day": this.data.hiddendTime3,
            "feature": "week",
            "shop_id": this.data.shop_id
          }
          this.dateinterface(data)
        }
       
      } else if (this.data.activeIndex == "2") {
       
        if (this.data.monthday == '' || this.data.monthday == "null" || this.data.monthday===undefined){
          wx.showToast({
            title: '请选择基准值',
            icon: 'none',
            duration: 2000
          })
        } else if (this.data.monthday1 == '' || this.data.monthday1 == "null" || this.data.monthday1 === undefined) {
          wx.showToast({
            title: '请选择时间',
            icon: 'none',
            duration: 2000
          })
        }else{
          // console.log(this.data.startTimemonth)
          // console.log(this.data.monthday)
          // console.log(this.data.monthday1)
          var choseday = []
          var baseday=[]
          choseday.push(this.data.startTimemonth + "-01", this.data.startTimemonth + '-' + this.data.monthday)
          baseday.push(this.data.startTimemonth1 + "-01", this.data.startTimemonth1 + '-' + this.data.monthday1)
        

        var data = {
          "base_day": choseday,
          "chose_day": baseday,
          "feature": "month",
          "shop_id": this.data.shop_id
        }
          this.dateinterface(data)
        }
      }
    
      
   }
     
    
  },


});